package com.jz.banner.screen.banner.ui

import android.annotation.SuppressLint
import android.content.Intent
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.jz.banner.screen.R
import com.jz.banner.screen.banner.net.GetApi
import com.jz.banner.screen.banner.utils.ConstantUtils
import com.jz.banner.screen.guard.LocalService
import com.jz.banner.screen.guard.RemoteService
import com.jz.banner.screen.image.ImageManger
import com.jz.banner.screen.permission.PermissionUtil
import com.jz.banner.screen.services.AppService
import com.jz.banner.screen.ui.activity.base.BaseActivity
import com.jz.banner.screen.utils.ARouterUtils
import com.jz.banner.screen.utils.CommUtils
import com.jz.banner.screen.utils.ZXingUtils
import com.jz.banner.screen.utils.eventbus.Event
import com.jz.banner.screen.utils.eventbus.EventCode
import kotlinx.android.synthetic.main.activity_allow_app.*

@Route(path = ARouterUtils.AllowAppActivity)
class AllowAppActivity : BaseActivity() {
    override fun getContentLayout(): Any = R.layout.activity_allow_app
    private var mUniqueId: String? = null

    companion object {
        var mIsShowAllow = false
    }

    override fun initView() {
        //加密 uuid
        mUniqueId = ConstantUtils.getPackageName().plus("-").plus(CommUtils.getAppUUid())
        mAllowIv.setImageBitmap(ZXingUtils.createQRCodeBitmap(mUniqueId, 2000))
        mAppUnitId.text = mUniqueId
        mVersionNameTv.text = CommUtils.getAppVersionName()
        //mAllowIv.postDelayed({ mTempStatues = 406 }, 5000)
    }

    override fun initData() {
        PermissionUtil.requestSomePermission(
            this, arrayOf(
                //感觉有些系统阉割个GPS,要不要考虑把下面定位干点。
                com.yanzhenjie.permission.Permission.ACCESS_FINE_LOCATION, com.yanzhenjie.permission.Permission.ACCESS_COARSE_LOCATION,
                //com.yanzhenjie.permission.Permission.READ_PHONE_STATE,
                com.yanzhenjie.permission.Permission.READ_EXTERNAL_STORAGE, com.yanzhenjie.permission.Permission.WRITE_EXTERNAL_STORAGE
            )
        ) { }
        ImageManger.setBgImage(this, GetApi.mAllowBG, mAllowBgIv)
    }

    @SuppressLint("CheckResult")
    override fun initListener() {
    }

    override fun isRegisterEventBus(): Boolean = true
    override fun receiveEvent(event: Event<Any>) {
        super.receiveEvent(event)
        when (event.code) {
            EventCode.FINISH_ALLOW_SCREEN -> goBack()
        }
    }

    override fun goBack() {
        ARouter.getInstance()
            .build(ARouterUtils.LoginActivity)
            .navigation()
        super.goBack()
    }

    override fun onResume() {
        super.onResume()
        mIsShowAllow = true
    }

    override fun onDestroy() {
        super.onDestroy()
        mIsShowAllow = false
    }

    override fun exit(): Boolean {
        try {
            if (isExit) {
                stopService(Intent(this, AppService::class.java))
                stopService(Intent(this, RemoteService::class.java))
                stopService(Intent(this, LocalService::class.java))
            }
        } catch (e: Exception) {
            d("退出失败:$e")
        }
        return super.exit()
    }

    override fun doubleQuit(): Boolean = true
}